package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.common.utils.file.FileUtils;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.domain.word.FireSafetyTicket;
import com.rzjm.exercise.module.domain.word.HighSafetyTicket;
import com.rzjm.exercise.module.domain.word.SpaceSafetyTicket;
import com.rzjm.exercise.module.domain.word.SpaceSafetyTicket2;
import com.rzjm.exercise.module.service.*;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import com.rzjm.exercise.sys.system.user.service.UserService;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.FileImageProvider;
import fr.opensagres.xdocreport.document.images.IImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * 动火安全作业票
 */

@Controller
@RequestMapping("/module/fireSafetyTicket")
public class FireSafetyTicketController {

    @Resource
    private FireWorkService fireWorkService;
    @Resource
    private FireWorkUserService fireWorkUserService;
    @Resource
    private FireWorkAnalysisService fireWorkAnalysisService;
    @Resource
    private FireWorkMeasuresService fireWorkMeasuresService;
    @Resource
    private GroupsService groupsService;
    @Resource
    private DeviceWeldingRecordService deviceWeldingRecordService;
    @Resource
    private UserService userService;
    @Resource
    private DepartmentService departmentService;
    @Value("${projecturl.uploadPath}")
    private String uploadPath;
    @Value("${projecturl.imgurl}")
    private String imgurl;

    private String DEFAULTIMGURL = "fireSafetyTicket/default.jpg";

    /**
     * 下载动火安全作业票
     */
    @GetMapping("/downloadTicket/{workId}")
    public void download(@PathVariable("workId") String workId, HttpServletResponse response) {

        FireWork fireWork = fireWorkService.findById(workId);
        if (fireWork.getRecordType() == 0) { // 动火作业票
            generateFireTicket(response, fireWork);
        } else if (fireWork.getRecordType() == 1) { // 受限空间作业票
            generateSpaceTicket2(response, fireWork);
//            generateSpaceTicket(response, fireWork);
        } else if (fireWork.getRecordType() == 2) {// 高空安全作业票
            generateHighTicket(response, fireWork);
        }

    }

    private void generateFireTicket(HttpServletResponse response, FireWork fireWork) {
        FireSafetyTicket fireSafetyTicket = new FireSafetyTicket();
        if (fireWork.getGroupId() != null){
            Groups groups = groupsService.findById(fireWork.getGroupId());
            if (groups == null){
                throw new RuntimeException("集团不存在！");
            }else if (StringUtils.isNotBlank(groups.getFireWorkTemplateUrl())){
                fireSafetyTicket.setTemplateUrl(uploadPath + groups.getFireWorkTemplateUrl());
                fireSafetyTicket.setGroupName(groups.getGroupName());
            }else {
                throw new RuntimeException("动火作业票模板不存在！");
            }
        }else {
            throw new RuntimeException("未绑定集团！");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        // 封装数据
        fireSafetyTicket.setNumber(fireWork.getRecordNo()); //编号
        fireSafetyTicket.setWorkApplyCompany(fireWork.getRegCompanyName()); //申请单位
        fireSafetyTicket.setWorkContent(fireWork.getWorkContent()); //作业内容
        fireSafetyTicket.setWorkPosition(fireWork.getFireAddress()); //动火地点、部位
        String workLevel = "";
        if (fireWork.getOpLevel() == null) {
            workLevel = "";
        } else if (fireWork.getOpLevel() == 1) {
            workLevel = "非生产区域动火";
        } else if (fireWork.getOpLevel() == 2) {
            workLevel = "生产区域动火";
        }
        fireSafetyTicket.setWorkLevel(workLevel); //作业级别
        String workWay = "";
        if (fireWork.getFireType() == null) {
            workWay = "";
        } else if (fireWork.getFireType() == 0) {
            workWay = "电焊";
        } else if (fireWork.getFireType() == 2) {
            workWay = "气焊(割)";
        } else if (fireWork.getFireType() == 1) {
            workWay = "等离子";
        } else if (fireWork.getFireType() == 3) {
            workWay = "喷灯";
        } else if (fireWork.getFireType() == 4) {
            workWay = "其他";
        }
        fireSafetyTicket.setWorkWay(workWay); //动火方式
        String workMan = "";
        String certificateNo = "";
        FireWorkUser fireWorkUser = fireWorkUserService.selectByUniqueColumn("fire_work_id", fireWork.getId());
        if (fireWorkUser != null) {
            workMan = (fireWorkUser.getWorkerName() == null ? "" : fireWorkUser.getWorkerName());
            certificateNo = (fireWorkUser.getWorkCardNum() == null ? "" : fireWorkUser.getWorkCardNum());
        }
        fireSafetyTicket.setWorkMan(workMan); //动火人及证书编号
        fireSafetyTicket.setCertificateNo(certificateNo); //动火人及证书编号
        fireSafetyTicket.setGuardian(fireWork.getSafeUserName()); //监护人
        fireSafetyTicket.setRiskIdentificationRsult(fireWork.getRiskIdentifiResult()); //风险辨识结果
        fireSafetyTicket.setWorkEffectiveDate((fireWork.getFireBeginTime() == null ? "" : sdf.format(fireWork.getFireBeginTime()) + "至")
                 + (fireWork.getFireEndTime() == null ? "" : sdf.format(fireWork.getFireEndTime()) + "止")); //动火作业实施时间
        String acWorkTime = "";
        QueryWrapper<DeviceWeldingRecord> deviceWeldingRecordQueryWrapper = new QueryWrapper<>();
        deviceWeldingRecordQueryWrapper.eq("fire_work_id", fireWork.getId());
        deviceWeldingRecordQueryWrapper.last("limit 1");
        DeviceWeldingRecord deviceWeldingRecord = deviceWeldingRecordService.getOne(deviceWeldingRecordQueryWrapper);
        if (fireWork.getCreateTime() != null && deviceWeldingRecord != null && deviceWeldingRecord.getRebackTime() != null){
            acWorkTime = sdf.format(fireWork.getCreateTime()) + "至" + sdf.format(deviceWeldingRecord.getRebackTime()) + "止";
        }
        fireSafetyTicket.setAcWorkEffectiveDate(acWorkTime);
//        fireSafetyTicket.setAcWorkEffectiveDate((fireWork.getCreateTime() == null ? "" :  sdf.format(fireWork.getCreateTime()))
//                +  (fireWork.getFinishTime() == null ? "" : ("至"
//                + sdf.format(fireWork.getFinishTime()) + "止"))); //实际动火作业实施时间
        fireSafetyTicket.setSecurityChargeSign(imgurl + fireWork.getSafeSignImg()); //确认人-
        fireSafetyTicket.setWorkApplySign(imgurl + fireWork.getRegSignImg()); //申请人


        fireSafetyTicket.setApproverSign(imgurl + fireWork.getSafeSignImg()); //作业前确认
        fireSafetyTicket.setAcceptSign(imgurl + fireWork.getCheckAcceptSignImg()); //完工后验收


        if (fireWork.getOpLevel() == 1) { //非生产区域
            // 批准人
            fireSafetyTicket.setSecuritySign(imgurl + fireWork.getCheckSignImg()); //批准人
            fireSafetyTicket.setSecurityOpinion(fireWork.getLastCheckState() == null ? ""
                    : (fireWork.getLastCheckState() == 1 ? "同意" : fireWork.getLastCheckState() == 0 ? "不同意" : "")); //批准人意见
            fireSafetyTicket.setSecuritySignTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //批准人签字时间
            // 审核人
            fireSafetyTicket.setWorkDirectorSign(imgurl + fireWork.getSubmitSignImg()); //审核人
            fireSafetyTicket.setWorkDirectorOpinion(fireWork.getLastSubmitState() == null ? ""
                    : (fireWork.getLastSubmitState() == 1 ? "同意" : fireWork.getLastSubmitState() == 0 ? "不同意" : "")); //审核人意见
            fireSafetyTicket.setWorkDirectorTime(fireWork.getLastSubmitTime() == null ? "" : sdf.format(fireWork.getLastSubmitTime())); //审核人签字时间
        } else if (fireWork.getOpLevel() == 2) {//生产区域
            // 批准人
            fireSafetyTicket.setSecuritySign(imgurl + fireWork.getSubmitSignImg()); //批准人
            fireSafetyTicket.setSecurityOpinion(fireWork.getLastSubmitState() == null ? ""
                    : (fireWork.getLastSubmitState() == 1 ? "同意" : fireWork.getLastSubmitState() == 0 ? "不同意" : "")); //批准人意见
            fireSafetyTicket.setSecuritySignTime(fireWork.getLastSubmitTime() == null ? "" : sdf.format(fireWork.getLastSubmitTime())); //批准人签字时间
            // 审核人
            fireSafetyTicket.setWorkDirectorSign(imgurl + fireWork.getCheckSignImg()); //审核人
            fireSafetyTicket.setWorkDirectorOpinion(fireWork.getLastCheckState() == null ? ""
                    : (fireWork.getLastCheckState() == 1 ? "同意" : fireWork.getLastCheckState() == 0 ? "不同意" : "")); //审核人意见
            fireSafetyTicket.setWorkDirectorTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //审核人签字时间
        }else {
            // 批准人
            fireSafetyTicket.setSecuritySign(imgurl + fireWork.getSubmitSignImg()); //批准人
            fireSafetyTicket.setSecurityOpinion(fireWork.getLastSubmitState() == null ? ""
                    : (fireWork.getLastSubmitState() == 1 ? "同意" : fireWork.getLastSubmitState() == 0 ? "不同意" : "")); //批准人意见
            fireSafetyTicket.setSecuritySignTime(fireWork.getLastSubmitTime() == null ? "" : sdf.format(fireWork.getLastSubmitTime())); //批准人签字时间
            // 审核人
            fireSafetyTicket.setWorkDirectorSign(imgurl + fireWork.getCheckSignImg()); //审核人
            fireSafetyTicket.setWorkDirectorOpinion(fireWork.getLastCheckState() == null ? ""
                    : (fireWork.getLastCheckState() == 1 ? "同意" : fireWork.getLastCheckState() == 0 ? "不同意" : "")); //审核人意见
            fireSafetyTicket.setWorkDirectorTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //审核人签字时间
        }


        fireSafetyTicket.setWorkApplyTime(fireWork.getCreateTime() == null ? "" : sdf.format(fireWork.getCreateTime())); //作业申请时间

        fireSafetyTicket.setApproverSignTime(fireWork.getSafeOpTime() == null ? "" : sdf.format(fireWork.getSafeOpTime())); //作业前确认签字时间
        fireSafetyTicket.setAcceptSignTime(fireWork.getCheckAcceptTime() == null ? "" : sdf.format(fireWork.getCheckAcceptTime())); //完工验收签字时间


        fireSafetyTicket.setMeasure9(getFireWorkMeasures(fireWork.getId(), 9).getAnswer());


        downloadSignTable(fireSafetyTicket, response);
    }

    private void generateSpaceTicket(HttpServletResponse response, FireWork fireWork){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SpaceSafetyTicket spaceSafetyTicket = new SpaceSafetyTicket();
        // 封装数据
        spaceSafetyTicket.setNumber(fireWork.getRecordNo()); //编号
        spaceSafetyTicket.setWorkApplyCompany(fireWork.getRegCompanyName()); //申请单位
        spaceSafetyTicket.setWorkApplyTime(fireWork.getCreateTime() == null ? "" : sdf.format(fireWork.getCreateTime())); //作业申请时间
        spaceSafetyTicket.setSpaceName(fireWork.getSpaceName()); //受限空间名称
        spaceSafetyTicket.setMediumName(fireWork.getSpaceMedium()); //受限空间内原有介质名称
        spaceSafetyTicket.setWorkContent(fireWork.getWorkContent()); //作业内容
        spaceSafetyTicket.setWorkCompany(fireWork.getRegCompanyName()); //作业单位
        UserDO userDO = userService.get(fireWork.getLastCheckSysUserId());
        spaceSafetyTicket.setWorkDirector(userDO == null ? "" : (userDO.getName() == null ? "" : userDO.getName())); //作业负责人
        FireWorkUser fireWorkUser = fireWorkUserService.selectByUniqueColumn("fire_work_id", fireWork.getId());
        if (fireWorkUser != null) {
            spaceSafetyTicket.setWorkMan(fireWorkUser.getWorkerName() == null ? "" : fireWorkUser.getWorkerName()); //作业人
        }else {
            spaceSafetyTicket.setWorkMan(""); //作业人
        }
        spaceSafetyTicket.setGuardian(fireWork.getRecordHeaderUser()); //监护人
        spaceSafetyTicket.setAssociatedWorkTicket(fireWork.getOtherSpecialWork()); //关联的其他特殊作业及安全作业票编号
        spaceSafetyTicket.setWorkEffectiveDate((fireWork.getFireBeginTime() == null ? "" : sdf.format(fireWork.getFireBeginTime()))
                + "至" + (fireWork.getFireEndTime() == null ? "" : sdf.format(fireWork.getFireEndTime()))); //作业实施时间
        List<FireWorkAnalysis> fireWorkAnalysisList = fireWorkAnalysisService
                .selectListByColumn("fire_work_id", fireWork.getId());
        if (fireWorkAnalysisList.size() > 0) {
            FireWorkAnalysis fireWorkAnalysis = fireWorkAnalysisList.get(0);

            spaceSafetyTicket.setHarmGas1(fireWorkAnalysis.getHarmfulGas()); //毒害气体名称1
            spaceSafetyTicket.setEharmGas1(fireWorkAnalysis.getHarmfulGasCriteria()); //毒害气体名称1合格标准
            spaceSafetyTicket.setHarmGas1D1(fireWorkAnalysis.getHarmfulData()); //毒害气体名称1分析数据

            spaceSafetyTicket.setHarmGas2(fireWorkAnalysis.getHarmfulGasTwo()); //毒害气体名称2
            spaceSafetyTicket.setEharmGas2(fireWorkAnalysis.getHarmfulGasCriteriaTwo()); //毒害气体名称2合格标准
            spaceSafetyTicket.setHarmGas2D1(fireWorkAnalysis.getHarmfulDataTwo()); //毒害气体名称2分析数据

            spaceSafetyTicket.setBurnGas1(fireWorkAnalysis.getFireGas()); //可燃气体名称1
            spaceSafetyTicket.setEburnGas1(fireWorkAnalysis.getFireGasCriteria()); //可燃气体名称1合格标准
            spaceSafetyTicket.setBurnGas1D1(fireWorkAnalysis.getFireGasData()); //可燃气体名称1分析数据

            spaceSafetyTicket.setBurnGas2(fireWorkAnalysis.getFireGasTwo()); //可燃气体名称2
            spaceSafetyTicket.setEburnGas2(fireWorkAnalysis.getFireGasCriteriaTwo()); //可燃气体名称2合格标准
            spaceSafetyTicket.setBurnGas2D1(fireWorkAnalysis.getFireGasDataTwo()); //可燃气体名称2分析数据

            spaceSafetyTicket.setOxygenCD1(fireWorkAnalysis.getOVolum()); //氧气含量
            spaceSafetyTicket.setTimeD1(sdf.format(fireWorkAnalysis.getCreateTime())); //取样分析时间
            spaceSafetyTicket.setPart1(fireWorkAnalysis.getAnalysisPart()); //分析部位
            spaceSafetyTicket.setAperson1(fireWorkAnalysis.getOpUser()); //分析人
        }
        if (fireWorkAnalysisList.size() > 1) {
            FireWorkAnalysis fireWorkAnalysis = fireWorkAnalysisList.get(1);
            spaceSafetyTicket.setHarmGas1D2(fireWorkAnalysis.getHarmfulData()); //毒害气体名称1分析数据

            spaceSafetyTicket.setHarmGas2D2(fireWorkAnalysis.getHarmfulDataTwo()); //毒害气体名称2分析数据

            spaceSafetyTicket.setBurnGas1D2(fireWorkAnalysis.getFireGasData()); //可燃气体名称1分析数据

            spaceSafetyTicket.setBurnGas2D2(fireWorkAnalysis.getFireGasDataTwo()); //可燃气体名称2分析数据

            spaceSafetyTicket.setOxygenCD2(fireWorkAnalysis.getOVolum()); //氧气含量
            spaceSafetyTicket.setTimeD2(sdf.format(fireWorkAnalysis.getAnalysisTime())); //取样分析时间
            spaceSafetyTicket.setPart2(fireWorkAnalysis.getAnalysisPart()); //分析部位
            spaceSafetyTicket.setAperson2(fireWorkAnalysis.getOpUser()); //分析人
        }
        spaceSafetyTicket.setSecurityChargeSign(imgurl + fireWork.getSafeSignImg()); //确认人-责任人
        spaceSafetyTicket.setSecurityGuardianSign(imgurl + fireWork.getHeaderSignImg()); //确认人-监护人
        spaceSafetyTicket.setWriter(imgurl + fireWork.getHeaderSignImg()); //编制人
        spaceSafetyTicket.setSafetyDisclosurer(userDO == null ? "" : (userDO.getName() == null ? "" : userDO.getName())); //安全交底人
        spaceSafetyTicket.setReceiveDisclosurer(fireWork.getRecordHeaderUser()); //接受交底人
        spaceSafetyTicket.setWorkDirectorOpinion(fireWork.getLastCheckState() == null ? "" : (fireWork.getLastCheckState() == 1 ? "同意作业。" : "不同意")); //作业负责人意见
        spaceSafetyTicket.setCompanyOpinion(fireWork.getSafeOpState() == null ? "" : (fireWork.getSafeOpState() == 1 ? "同意作业。" : "不同意")); //所在单位意见
//        highSafetyTicket.setSecurityOpinion(fireWork.getSafeOpState() == null ? "" : (fireWork.getSafeOpState() == 1 ? "同意作业。" : "不同意")); //安全管理部门意见
//        highSafetyTicket.setApproverOpinion(fireWork.getSafeOpState() == null ? "" : (fireWork.getSafeOpState() == 1 ? "同意作业。" : "不同意")); //动火审批人意见
        spaceSafetyTicket.setAccept(fireWork.getLastCheckState() == null ? "" : (fireWork.getLastCheckState() == 1 ? "受限空间作业已完成，作业现场已清理。。" : "未完成")); //完工验收
        spaceSafetyTicket.setWorkDirectorSign(imgurl + fireWork.getCheckSignImg()); //作业负责人意见签字
        spaceSafetyTicket.setCompanySign(imgurl + fireWork.getSafeSignImg()); //所在单位意见签字
//        highSafetyTicket.setSecuritySign(imgurl + fireWork.getSafeSignImg()); //审核部门意见意见签字
//        highSafetyTicket.setApproverSign(imgurl + fireWork.getSafeSignImg()); //审批部门意见意见签字
        spaceSafetyTicket.setAcceptSign(imgurl + fireWork.getCheckSignImg()); //完工验收签字
        spaceSafetyTicket.setWorkDirectorTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //作业负责人意见签字时间
        spaceSafetyTicket.setCompanySignTime(fireWork.getSafeOpTime() == null ? "" : sdf.format(fireWork.getSafeOpTime())); //所在单位意见签字时间
//        highSafetyTicket.setSecuritySignTime(fireWork.getSafeOpTime() == null ? "" : sdf.format(fireWork.getSafeOpTime())); //安全管理部门意见签字时间
//        highSafetyTicket.setApproverSignTime(fireWork.getSafeOpTime() == null ? "" : sdf.format(fireWork.getSafeOpTime())); //动火审批人意见签字时间
        spaceSafetyTicket.setAcceptSignTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //完工验收签字时间

        spaceSafetyTicket.setMeasure19(getFireWorkMeasures(fireWork.getId(), 19).getAnswer());
        spaceSafetyTicket.setMeasure20(getFireWorkMeasures(fireWork.getId(), 20).getAnswer());
        spaceSafetyTicket.setMeasure21(getFireWorkMeasures(fireWork.getId(), 21).getAnswer());
        spaceSafetyTicket.setMeasure22(getFireWorkMeasures(fireWork.getId(), 22).getAnswer());
        spaceSafetyTicket.setMeasure23(getFireWorkMeasures(fireWork.getId(), 23).getAnswer());
        spaceSafetyTicket.setMeasure24(getFireWorkMeasures(fireWork.getId(), 24).getAnswer());
        spaceSafetyTicket.setMeasure25(getFireWorkMeasures(fireWork.getId(), 25).getAnswer());
        spaceSafetyTicket.setMeasure26(getFireWorkMeasures(fireWork.getId(), 26).getAnswer());
        spaceSafetyTicket.setMeasure27(getFireWorkMeasures(fireWork.getId(), 27).getAnswer());
        spaceSafetyTicket.setMeasure28(getFireWorkMeasures(fireWork.getId(), 28).getAnswer());
        spaceSafetyTicket.setMeasure46(getFireWorkMeasures(fireWork.getId(), 46).getAnswer());
        spaceSafetyTicket.setMeasure47(getFireWorkMeasures(fireWork.getId(), 47).getAnswer());
        spaceSafetyTicket.setMeasure48(getFireWorkMeasures(fireWork.getId(), 48).getAnswer());
        spaceSafetyTicket.setMeasure49(getFireWorkMeasures(fireWork.getId(), 49).getAnswer());
        spaceSafetyTicket.setMeasure50(getFireWorkMeasures(fireWork.getId(), 50).getAnswer());
        spaceSafetyTicket.setMeasure51(getFireWorkMeasures(fireWork.getId(), 51).getAnswer());
        spaceSafetyTicket.setMeasure51(getFireWorkMeasures(fireWork.getId(), 52).getAnswer());
        spaceSafetyTicket.setMeasure51(getFireWorkMeasures(fireWork.getId(), 53).getAnswer());

        downloadSpaceSignTable(spaceSafetyTicket, response);

    }

    private void generateHighTicket(HttpServletResponse response, FireWork fireWork){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        HighSafetyTicket highSafetyTicket = new HighSafetyTicket();
        // 封装数据
        if (fireWork.getGroupId() != null){
            Groups groups = groupsService.findById(fireWork.getGroupId());
            if (groups == null){
                throw new RuntimeException("集团不存在！");
            }else if (StringUtils.isNotBlank(groups.getHighWorkTemplateUrl())){
                highSafetyTicket.setTemplateUrl(uploadPath + groups.getHighWorkTemplateUrl());
                highSafetyTicket.setGroupName(groups.getGroupName());
            }else {
                throw new RuntimeException("高空作业票模板不存在！");
            }
        }else {
            throw new RuntimeException("未绑定集团！");
        }
        highSafetyTicket.setRecordNo(fireWork.getRecordNo()); //申请单位
        highSafetyTicket.setRegCompanyName(fireWork.getRegCompanyName()); //申请单位
        highSafetyTicket.setRegUserName(fireWork.getRegUserName()); //申请人
        highSafetyTicket.setWorkApplyTime(fireWork.getCreateTime() == null ? "" : sdf2.format(fireWork.getCreateTime())); //作业申请时间
        highSafetyTicket.setWorkContent(fireWork.getWorkContent()); //作业内容
        highSafetyTicket.setWorkHeight(fireWork.getWorkHeight()); //作业高度
        highSafetyTicket.setWorkPosition(fireWork.getFireAddress()); //作业地点

        List<FireWorkUser> fireWorkUsers = fireWorkUserService.selectListByColumn("fire_work_id", fireWork.getId());
        highSafetyTicket.setWorkManDept("");
        if (fireWorkUsers != null && fireWorkUsers.size() > 0) {
            if (fireWorkUsers.get(0).getIsOut() != null && fireWorkUsers.get(0).getIsOut() == 0){
                highSafetyTicket.setWorkManDept("公司员工");
            }else if (fireWorkUsers.get(0).getIsOut() != null && fireWorkUsers.get(0).getIsOut() == 1){
                highSafetyTicket.setWorkManDept("外协");
            }
            String name = fireWorkUsers.stream().map(u -> u.getWorkerName()).collect(Collectors.joining(","));
            highSafetyTicket.setWorkMan(name == null ? "" : name); //作业人
        }else {
            highSafetyTicket.setWorkMan(""); //作业人
        }
        highSafetyTicket.setWorkEffectiveDate((fireWork.getFireBeginTime() == null ? "" : sdf.format(fireWork.getFireBeginTime()))
                + "至" + (fireWork.getFireEndTime() == null ? "" : sdf.format(fireWork.getFireEndTime()))); //作业实施时间

        highSafetyTicket.setSupervisorSign(imgurl + fireWork.getSafeSignImg()); //督导员签字
        highSafetyTicket.setProductionManagerSign(imgurl + fireWork.getCheckSignImg()); //生产经理签字
        highSafetyTicket.setSafeSupervisorSign(imgurl + fireWork.getSafeSignImg()); //安全督导员签字
//        highSafetyTicket.setWorkDirectorOpinion(fireWork.getLastCheckState() == null ? "" : (fireWork.getLastCheckState() == 1 ? "同意作业。" : "不同意")); //作业负责人意见

        highSafetyTicket.setProductionManagerSignTime(fireWork.getSafeOpTime() == null ? "" : sdf.format(fireWork.getSafeOpTime())); //生产经理签字时间
        highSafetyTicket.setSafeSupervisorTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //安全督导员签字时间

        highSafetyTicket.setMeasure29(getFireWorkMeasures(fireWork.getId(), 29).getAnswer());
        highSafetyTicket.setMeasure30(getFireWorkMeasures(fireWork.getId(), 30).getAnswer());
        highSafetyTicket.setMeasure31(getFireWorkMeasures(fireWork.getId(), 31).getAnswer());
        highSafetyTicket.setMeasure32(getFireWorkMeasures(fireWork.getId(), 32).getAnswer());
        highSafetyTicket.setMeasure33(getFireWorkMeasures(fireWork.getId(), 33).getAnswer());
        highSafetyTicket.setMeasure34(getFireWorkMeasures(fireWork.getId(), 34).getAnswer());
        highSafetyTicket.setMeasure35(getFireWorkMeasures(fireWork.getId(), 35).getAnswer());


        downloadHighSignTable(highSafetyTicket, response);
    }

    public void downloadSignTable(FireSafetyTicket fireSafetyTicket, HttpServletResponse response) {
        try {
            String sou = String.valueOf(System.currentTimeMillis());
            String tempFileName = "动火安全作业票.docx";

            String path = fireSafetyTicket.getTemplateUrl();
//            String path = uploadPath + File.separator + "fireSafetyTicket" + File.separator + tempFileName;
//            String path = "D:\\template\\动火安全作业票.docx";
            System.out.println("文件路径 :" + path);

            File template = new File(path);
            InputStream ins = new FileInputStream(template);

            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsImage("acceptSign");
            metadata.addFieldAsImage("approverSign");
            metadata.addFieldAsImage("securityChargeSign");
            metadata.addFieldAsImage("securitySign");
            metadata.addFieldAsImage("workApplySign");
            metadata.addFieldAsImage("workDirectorSign");

//            metadata.addFieldAsImage("imageNotExistsAndRemoveImageTemplate", NullImageBehaviour.RemoveImageTemplate);
//            metadata.addFieldAsImage("imageNotExistsAndKeepImageTemplate", NullImageBehaviour.KeepImageTemplate);
            // 封装数据
            context = setWordValue(context, fireSafetyTicket, metadata);

            //创建字段元数据
//            String fileName = uploadPath + File.separator + "fireSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date())
//                    + File.separator + "动火安全作业票_" + sou + ".docx";
            File filPath = new File(uploadPath + File.separator + "fireSafetyTicket"+ File.separator + fireSafetyTicket.getGroupName() + File.separator + DateUtils.getMonthStr(new Date()));
//            File filPath = new File("D:\\template\\" + DateUtils.getMonthStr(new Date()));
            if (!filPath.exists()) {
                filPath.mkdirs();

            }
            String fileName = filPath + File.separator  + "动火安全作业票_" + sou + ".docx";
            System.out.println("fileName: " + fileName);
            File file = new File(fileName);
            FileOutputStream out = new FileOutputStream(file);
            report.process(context, out);
            out.flush();
            out.close();
            ins.close();

            try {
                if (file != null) {
                    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    FileUtils.setAttachmentResponseHeader(response, file.getName());
                    FileUtils.writeToStream(file, response.getOutputStream());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void downloadHighSignTable(HighSafetyTicket highSafetyTicket, HttpServletResponse response) {
        try {
            String sou = String.valueOf(System.currentTimeMillis());
            String tempFileName = "高处安全作业票.docx";

//            String path = uploadPath + File.separator + "highSafetyTicket" + File.separator + tempFileName;
            String path = highSafetyTicket.getTemplateUrl();
//            String path = "D:\\template\\动火安全作业票.docx";
            System.out.println("文件路径 :" + path);

            File template = new File(path);
            InputStream ins = new FileInputStream(template);

            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsImage("supervisorSign");
            metadata.addFieldAsImage("productionManagerSign");
            metadata.addFieldAsImage("safeSupervisorSign");


//            metadata.addFieldAsImage("imageNotExistsAndRemoveImageTemplate", NullImageBehaviour.RemoveImageTemplate);
//            metadata.addFieldAsImage("imageNotExistsAndKeepImageTemplate", NullImageBehaviour.KeepImageTemplate);
            // 封装数据
            context = setHighWordValue(context, highSafetyTicket, metadata);

            //创建字段元数据
//            String fileName = uploadPath + File.separator + "highSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date())
//                    + File.separator + "高处安全作业票_" + sou + ".docx";
//            File filPath = new File(uploadPath + File.separator + "fireSafetyTicket"+ File.separator + fireSafetyTicket.getGroupName() + File.separator + DateUtils.getMonthStr(new Date()));
//            String fileName = uploadPath + File.separator + "highSafetyTicket" + File.separator + "高处安全作业票_" + sou + ".docx";
            File filPath = new File(uploadPath + File.separator + "highSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date()));
//            File filPath = new File("D:\\template\\" + DateUtils.getMonthStr(new Date()));
            if (!filPath.exists()) {
                filPath.mkdir();
            }
            String fileName = filPath + File.separator  + "高处安全作业票_" + sou + ".docx";
            System.out.println("fileName: " + fileName);
            File file = new File(fileName);
            FileOutputStream out = new FileOutputStream(file);
            report.process(context, out);
            out.flush();
            out.close();
            ins.close();
            try {
                if (file != null) {
                    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    FileUtils.setAttachmentResponseHeader(response, file.getName());
                    FileUtils.writeToStream(file, response.getOutputStream());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void downloadSpaceSignTable(SpaceSafetyTicket spaceSafetyTicket, HttpServletResponse response) {
        try {
            String sou = String.valueOf(System.currentTimeMillis());
            String tempFileName = "受限空间安全作业票.docx";

            String path = uploadPath + File.separator + "spaceSafetyTicket" + File.separator + tempFileName;
//            String path = "D:\\template\\动火安全作业票.docx";
            System.out.println("文件路径 :" + path);

            File template = new File(path);
            InputStream ins = new FileInputStream(template);

            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsImage("acceptSign");
            metadata.addFieldAsImage("companySign");
            metadata.addFieldAsImage("securityChargeSign");
            metadata.addFieldAsImage("securityGuardianSign");
            metadata.addFieldAsImage("workDirectorSign");
            metadata.addFieldAsImage("writer");


//            metadata.addFieldAsImage("imageNotExistsAndRemoveImageTemplate", NullImageBehaviour.RemoveImageTemplate);
//            metadata.addFieldAsImage("imageNotExistsAndKeepImageTemplate", NullImageBehaviour.KeepImageTemplate);
            // 封装数据
            context = setSpaceWordValue(context, spaceSafetyTicket, metadata);

            //创建字段元数据
//            String fileName = uploadPath + File.separator + "highSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date())
//                    + File.separator + "受限空间安全作业票_" + sou + ".docx";
//            String fileName = uploadPath + File.separator + "spaceSafetyTicket" + File.separator + "受限空间安全作业票_" + sou + ".docx";
            File filPath = new File(uploadPath + File.separator + "spaceSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date()));
//            File filPath = new File("D:\\template\\" + DateUtils.getMonthStr(new Date()));
            if (!filPath.exists()) {
                filPath.mkdir();
            }
            String fileName = filPath + File.separator  + "受限空间安全作业票_" + sou + ".docx";
            System.out.println("fileName: " + fileName);
            File file = new File(fileName);
            FileOutputStream out = new FileOutputStream(file);
            report.process(context, out);
            out.flush();
            out.close();
            ins.close();
            try {
                if (file != null) {
                    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    FileUtils.setAttachmentResponseHeader(response, file.getName());
                    FileUtils.writeToStream(file, response.getOutputStream());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * word模板赋值
     *
     * @param context
     */
    private IContext setWordValue(IContext context, FireSafetyTicket fireSafetyTicket, FieldsMetadata metadata) {
        try {
            context.put("number", fireSafetyTicket.getNumber() == null ? "" : fireSafetyTicket.getNumber()); //编号
            context.put("workApplyCompany", fireSafetyTicket.getWorkApplyCompany() == null ? "" : fireSafetyTicket.getWorkApplyCompany()); //申请单位
            context.put("workApplyTime", fireSafetyTicket.getWorkApplyTime() == null ? "" : fireSafetyTicket.getWorkApplyTime()); //作业申请时间
            context.put("workContent", fireSafetyTicket.getWorkContent() == null ? "" : fireSafetyTicket.getWorkContent()); //作业内容
            context.put("workPosition", fireSafetyTicket.getWorkPosition() == null ? "" : fireSafetyTicket.getWorkPosition()); //动火地点、部位
            context.put("workLevel", fireSafetyTicket.getWorkLevel() == null ? "" : fireSafetyTicket.getWorkLevel()); //作业级别
            context.put("workWay", fireSafetyTicket.getWorkWay() == null ? "" : fireSafetyTicket.getWorkWay()); //动火方式
            context.put("workMan", fireSafetyTicket.getWorkMan() == null ? "" : fireSafetyTicket.getWorkMan()); //动火人及证书编号
            context.put("certificateNo", fireSafetyTicket.getCertificateNo() == null ? "" : fireSafetyTicket.getCertificateNo()); //动火人及证书编号

            context.put("riskIdentificationRsult", fireSafetyTicket.getRiskIdentificationRsult() == null ? "" : fireSafetyTicket.getRiskIdentificationRsult()); //风险辨识结果
            context.put("workEffectiveDate", fireSafetyTicket.getWorkEffectiveDate() == null ? "" : fireSafetyTicket.getWorkEffectiveDate()); //动火作业实施时间
            context.put("acWorkEffectiveDate", fireSafetyTicket.getAcWorkEffectiveDate() == null ? "" : fireSafetyTicket.getAcWorkEffectiveDate()); //动火作业实施时间

            if (StringUtils.isNotBlank(fireSafetyTicket.getSecurityChargeSign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getSecurityChargeSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityChargeSign", iImageProvider); //确认人-责任人
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityChargeSign", iImageProvider); //确认人-责任人
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("securityChargeSign", iImageProvider); //确认人-责任人
            }

            context.put("guardian", fireSafetyTicket.getGuardian() == null ? "" : fireSafetyTicket.getGuardian()); //监护人
            context.put("workDirectorOpinion", fireSafetyTicket.getWorkDirectorOpinion() == null ? "" : fireSafetyTicket.getWorkDirectorOpinion()); //作业负责人意见
            context.put("securityOpinion", fireSafetyTicket.getSecurityOpinion() == null ? "" : fireSafetyTicket.getSecurityOpinion()); //安全管理部门意见
            context.put("approverOpinion", fireSafetyTicket.getApproverOpinion() == null ? "" : fireSafetyTicket.getApproverOpinion()); //动火审批人意见
            context.put("accept", fireSafetyTicket.getAccept() == null ? "" : fireSafetyTicket.getAccept()); //完工验收
            if (StringUtils.isNotBlank(fireSafetyTicket.getWorkApplySign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getWorkApplySign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workApplySign", iImageProvider); //作业负责人意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workApplySign", iImageProvider); //作业负责人意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("workApplySign", iImageProvider); //作业负责人意见签字
            }
            if (StringUtils.isNotBlank(fireSafetyTicket.getWorkDirectorSign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getWorkDirectorSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
            }
            if (StringUtils.isNotBlank(fireSafetyTicket.getSecuritySign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getSecuritySign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securitySign", iImageProvider); //安全管理部门意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securitySign", iImageProvider); //安全管理部门意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("securitySign", iImageProvider); //安全管理部门意见签字
            }
            if (StringUtils.isNotBlank(fireSafetyTicket.getApproverSign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getApproverSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("approverSign", iImageProvider); //动火审批人意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("approverSign", iImageProvider); //动火审批人意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("approverSign", iImageProvider); //动火审批人意见签字
            }

            if (StringUtils.isNotBlank(fireSafetyTicket.getAcceptSign())) {
                try {
                    File file1 = UrltoFile(fireSafetyTicket.getAcceptSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //完工验收签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //完工验收签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("acceptSign", iImageProvider); //完工验收签字
            }
            context.put("workDirectorTime", fireSafetyTicket.getWorkDirectorTime() == null ? "" : fireSafetyTicket.getWorkDirectorTime()); //作业负责人意见签字时间
            context.put("securitySignTime", fireSafetyTicket.getSecuritySignTime() == null ? "" : fireSafetyTicket.getSecuritySignTime()); //安全管理部门意见签字时间
            context.put("approverSignTime", fireSafetyTicket.getApproverSignTime() == null ? "" : fireSafetyTicket.getApproverSignTime()); //动火审批人意见签字时间
            context.put("acceptSignTime", fireSafetyTicket.getAcceptSignTime() == null ? "" : fireSafetyTicket.getAcceptSignTime()); //完工验收签字时间


            context.put("measure9", fireSafetyTicket.getMeasure9() == null ? "" : fireSafetyTicket.getMeasure9());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return context;
    }

    private IContext setHighWordValue(IContext context, HighSafetyTicket highSafetyTicket, FieldsMetadata metadata) {
        try {
            context.put("recordNo", highSafetyTicket.getRecordNo() == null ? "" : highSafetyTicket.getRecordNo()); //申请人
            context.put("regCompanyName", highSafetyTicket.getRegCompanyName() == null ? "" : highSafetyTicket.getRegCompanyName()); //申请人
            context.put("regUserName", highSafetyTicket.getRegUserName() == null ? "" : highSafetyTicket.getRegUserName()); //申请人
            context.put("workManDept", highSafetyTicket.getWorkManDept() == null ? "" : highSafetyTicket.getWorkManDept()); //作业人单位
            context.put("workApplyTime", highSafetyTicket.getWorkApplyTime() == null ? "" : highSafetyTicket.getWorkApplyTime()); //作业申请时间
            context.put("workContent", highSafetyTicket.getWorkContent() == null ? "" : highSafetyTicket.getWorkContent()); //作业内容
            context.put("workPosition", highSafetyTicket.getWorkPosition() == null ? "" : highSafetyTicket.getWorkPosition()); //地点、部位
            context.put("workHeight", highSafetyTicket.getWorkHeight() == null ? "" : highSafetyTicket.getWorkHeight()); //作业高度
            context.put("workMan", highSafetyTicket.getWorkMan() == null ? "" : highSafetyTicket.getWorkMan()); //作业人
            context.put("workEffectiveDate", highSafetyTicket.getWorkEffectiveDate() == null ? "" : highSafetyTicket.getWorkEffectiveDate()); //动火作业实施时间
            if (StringUtils.isNotBlank(highSafetyTicket.getSupervisorSign())) {
                try {
                    File file1 = UrltoFile(highSafetyTicket.getSupervisorSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("supervisorSign", iImageProvider); //督导员
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("supervisorSign", iImageProvider); //督导员
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("supervisorSign", iImageProvider); //督导员
            }

            if (StringUtils.isNotBlank(highSafetyTicket.getProductionManagerSign())) {
                try {
                    File file1 = UrltoFile(highSafetyTicket.getProductionManagerSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("productionManagerSign", iImageProvider); //生产经理
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("productionManagerSign", iImageProvider); //生产经理
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("productionManagerSign", iImageProvider); //生产经理
            }

            if (StringUtils.isNotBlank(highSafetyTicket.getSafeSupervisorSign())) {
                try {
                    File file1 = UrltoFile(highSafetyTicket.getSafeSupervisorSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("safeSupervisorSign", iImageProvider); //安全督导员
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("safeSupervisorSign", iImageProvider); //安全督导员
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("safeSupervisorSign", iImageProvider); //安全督导员
            }

            context.put("productionManagerSignTime", highSafetyTicket.getProductionManagerSignTime() == null ? "" : highSafetyTicket.getProductionManagerSignTime()); //生产经理签字时间
            context.put("safeSupervisorTime", highSafetyTicket.getSafeSupervisorTime() == null ? "" : highSafetyTicket.getSafeSupervisorTime()); //安全督导员签字时间

            context.put("measure29", highSafetyTicket.getMeasure29() == null ? "" : ("1".equals(highSafetyTicket.getMeasure29())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure29()) ? "×" : "")));
            context.put("measure30", highSafetyTicket.getMeasure30() == null ? "" : ("1".equals(highSafetyTicket.getMeasure30())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure30()) ? "×" : "")));
            context.put("measure31", highSafetyTicket.getMeasure31() == null ? "" : ("1".equals(highSafetyTicket.getMeasure31())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure31()) ? "×" : "")));
            context.put("measure32", highSafetyTicket.getMeasure32() == null ? "" : ("1".equals(highSafetyTicket.getMeasure32())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure32()) ? "×" : "")));
            context.put("measure33", highSafetyTicket.getMeasure33() == null ? "" : ("1".equals(highSafetyTicket.getMeasure33())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure33()) ? "×" : "")));
            context.put("measure34", highSafetyTicket.getMeasure34() == null ? "" : ("1".equals(highSafetyTicket.getMeasure34())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure34()) ? "×" : "")));
            context.put("measure35", highSafetyTicket.getMeasure35() == null ? "" : ("1".equals(highSafetyTicket.getMeasure35())
                    ? "√" : ("0".equals(highSafetyTicket.getMeasure35()) ? "×" : "")));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return context;
    }

    private IContext setSpaceWordValue(IContext context, SpaceSafetyTicket spaceSafetyTicket, FieldsMetadata metadata) {
        try {
            context.put("number", spaceSafetyTicket.getNumber() == null ? "" : spaceSafetyTicket.getNumber()); //编号
            context.put("workApplyCompany", spaceSafetyTicket.getWorkApplyCompany() == null ? "" : spaceSafetyTicket.getWorkApplyCompany()); //申请单位
            context.put("workApplyTime", spaceSafetyTicket.getWorkApplyTime() == null ? "" : spaceSafetyTicket.getWorkApplyTime()); //作业申请时间
            context.put("spaceName", spaceSafetyTicket.getSpaceName() == null ? "" : spaceSafetyTicket.getSpaceName()); //受限空间名称
            context.put("mediumName", spaceSafetyTicket.getMediumName() == null ? "" : spaceSafetyTicket.getMediumName()); //受限空间内原有介质名称
            context.put("workContent", spaceSafetyTicket.getWorkContent() == null ? "" : spaceSafetyTicket.getWorkContent()); //作业内容
            context.put("workMan", spaceSafetyTicket.getWorkMan() == null ? "" : spaceSafetyTicket.getWorkMan()); //作业人
            context.put("workDirector", spaceSafetyTicket.getWorkDirector() == null ? "" : spaceSafetyTicket.getWorkDirector()); //作业负责人
            context.put("workCompany", spaceSafetyTicket.getWorkCompany() == null ? "" : spaceSafetyTicket.getWorkCompany()); //作业单位
            context.put("guardian", spaceSafetyTicket.getGuardian() == null ? "" : spaceSafetyTicket.getGuardian()); //监护人
            context.put("associatedWorkTicket", spaceSafetyTicket.getAssociatedWorkTicket() == null ? "" : spaceSafetyTicket.getAssociatedWorkTicket()); //关联的其他特殊作业及安全作业票编号
            context.put("workEffectiveDate", spaceSafetyTicket.getWorkEffectiveDate() == null ? "" : spaceSafetyTicket.getWorkEffectiveDate()); //动火作业实施时间
            context.put("harmGas1", spaceSafetyTicket.getHarmGas1() == null ? "" : spaceSafetyTicket.getHarmGas1());
            context.put("EharmGas1", spaceSafetyTicket.getEharmGas1() == null ? "" : spaceSafetyTicket.getEharmGas1());
            context.put("harmGas1D1", spaceSafetyTicket.getHarmGas1D1() == null ? "" : spaceSafetyTicket.getHarmGas1D1());
            context.put("harmGas1D2", spaceSafetyTicket.getHarmGas1D2() == null ? "" : spaceSafetyTicket.getHarmGas1D2());
            context.put("harmGas2", spaceSafetyTicket.getHarmGas2() == null ? "" : spaceSafetyTicket.getHarmGas2());
            context.put("EharmGas2", spaceSafetyTicket.getEharmGas2() == null ? "" : spaceSafetyTicket.getEharmGas2());
            context.put("harmGas2D1", spaceSafetyTicket.getHarmGas2D1() == null ? "" : spaceSafetyTicket.getHarmGas2D1());
            context.put("harmGas2D2", spaceSafetyTicket.getHarmGas2D2() == null ? "" : spaceSafetyTicket.getHarmGas2D2());
            context.put("burnGas1", spaceSafetyTicket.getBurnGas1() == null ? "" : spaceSafetyTicket.getBurnGas1());
            context.put("EburnGas1", spaceSafetyTicket.getEburnGas1() == null ? "" : spaceSafetyTicket.getEburnGas1());
            context.put("burnGas1D1", spaceSafetyTicket.getBurnGas1D1() == null ? "" : spaceSafetyTicket.getBurnGas1D1());
            context.put("burnGas1D2", spaceSafetyTicket.getBurnGas1D2() == null ? "" : spaceSafetyTicket.getBurnGas1D2());
            context.put("burnGas2", spaceSafetyTicket.getBurnGas2() == null ? "" : spaceSafetyTicket.getBurnGas2());
            context.put("EburnGas2", spaceSafetyTicket.getEburnGas2() == null ? "" : spaceSafetyTicket.getEburnGas2());
            context.put("burnGas2D1", spaceSafetyTicket.getBurnGas2D1() == null ? "" : spaceSafetyTicket.getBurnGas2D1());
            context.put("burnGas2D2", spaceSafetyTicket.getBurnGas2D2() == null ? "" : spaceSafetyTicket.getBurnGas2D2());
            context.put("oxygenCD1", spaceSafetyTicket.getOxygenCD1() == null ? "" : spaceSafetyTicket.getOxygenCD1());
            context.put("oxygenCD2", spaceSafetyTicket.getOxygenCD2() == null ? "" : spaceSafetyTicket.getOxygenCD2());
            context.put("timeD1", spaceSafetyTicket.getTimeD1() == null ? "" : spaceSafetyTicket.getTimeD1());
            context.put("timeD2", spaceSafetyTicket.getTimeD2() == null ? "" : spaceSafetyTicket.getTimeD2());
            context.put("part1", spaceSafetyTicket.getPart1() == null ? "" : spaceSafetyTicket.getPart1());
            context.put("part2", spaceSafetyTicket.getPart2() == null ? "" : spaceSafetyTicket.getPart2());
            context.put("Aperson1", spaceSafetyTicket.getAperson1() == null ? "" : spaceSafetyTicket.getAperson1());
            context.put("Aperson2", spaceSafetyTicket.getAperson2() == null ? "" : spaceSafetyTicket.getAperson2());
            if (StringUtils.isNotBlank(spaceSafetyTicket.getSecurityChargeSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getSecurityChargeSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityChargeSign", iImageProvider); //确认人-责任人
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityChargeSign", iImageProvider); //确认人-责任人
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("securityChargeSign", iImageProvider); //确认人-责任人
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getSecurityGuardianSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getSecurityGuardianSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityGuardianSign", iImageProvider); //确认人-责任人
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("securityGuardianSign", iImageProvider); //确认人-责任人
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("securityGuardianSign", iImageProvider); //确认人-责任人
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getWriter())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getWriter());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("writer", iImageProvider); //编制人
                } catch (Exception e) {
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("writer", iImageProvider); //编制人
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("writer", iImageProvider); //确认人-监护人
            }
            context.put("safetyDisclosurer", spaceSafetyTicket.getSafetyDisclosurer()); //安全交底人
            context.put("receiveDisclosurer", spaceSafetyTicket.getReceiveDisclosurer()); //接受交底人
            context.put("workDirectorOpinion", spaceSafetyTicket.getWorkDirectorOpinion() == null ? "" : spaceSafetyTicket.getWorkDirectorOpinion()); //作业负责人意见
            context.put("companyOpinion", spaceSafetyTicket.getCompanyOpinion() == null ? "" : spaceSafetyTicket.getCompanyOpinion()); //所在单位意见
            context.put("accept", spaceSafetyTicket.getAccept() == null ? "" : spaceSafetyTicket.getAccept()); //完工验收
            if (StringUtils.isNotBlank(spaceSafetyTicket.getWorkDirectorSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getWorkDirectorSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("workDirectorSign", iImageProvider); //作业负责人意见签字
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getCompanySign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getCompanySign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("companySign", iImageProvider); //所在单位意见签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("companySign", iImageProvider); //所在单位意见签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("companySign", iImageProvider); //所在单位意见签字
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getAcceptSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getAcceptSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //完工验收签字
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //完工验收签字
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("acceptSign", iImageProvider); //完工验收签字
            }
            context.put("workDirectorTime", spaceSafetyTicket.getWorkDirectorTime() == null ? "" : spaceSafetyTicket.getWorkDirectorTime()); //作业负责人意见签字时间
            context.put("companySignTime", spaceSafetyTicket.getCompanySignTime() == null ? "" : spaceSafetyTicket.getCompanySignTime()); //所在单位意见签字时间
            context.put("acceptSignTime", spaceSafetyTicket.getAcceptSignTime() == null ? "" : spaceSafetyTicket.getAcceptSignTime()); //完工验收签字时间

            context.put("measure19", spaceSafetyTicket.getMeasure19() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure19())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure19()) ? "×" : "")));
            context.put("measure20", spaceSafetyTicket.getMeasure20() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure20())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure20()) ? "×" : "")));
            context.put("measure21", spaceSafetyTicket.getMeasure21() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure21())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure21()) ? "×" : "")));
            context.put("measure22", spaceSafetyTicket.getMeasure22() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure22())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure22()) ? "×" : "")));
            context.put("measure23", spaceSafetyTicket.getMeasure23() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure23())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure23()) ? "×" : "")));
            context.put("measure24", spaceSafetyTicket.getMeasure24() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure24())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure24()) ? "×" : "")));
            context.put("measure25", spaceSafetyTicket.getMeasure25() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure25())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure25()) ? "×" : "")));
            context.put("measure26", spaceSafetyTicket.getMeasure26() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure26())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure26()) ? "×" : "")));
            context.put("measure27", spaceSafetyTicket.getMeasure27() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure27())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure27()) ? "×" : "")));
            context.put("measure28", spaceSafetyTicket.getMeasure28() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure28())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure28()) ? "×" : "")));
            context.put("measure50", spaceSafetyTicket.getMeasure50() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure50())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure50()) ? "×" : "")));
            context.put("measure51", spaceSafetyTicket.getMeasure51() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure51())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure51()) ? "×" : "")));
            context.put("measure52", spaceSafetyTicket.getMeasure52() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure52())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure52()) ? "×" : "")));
            context.put("measure53", spaceSafetyTicket.getMeasure53() == null ? "" : spaceSafetyTicket.getMeasure53());
            context.put("measure46", spaceSafetyTicket.getMeasure46() == null ? "" : spaceSafetyTicket.getMeasure46());
            context.put("measure47", spaceSafetyTicket.getMeasure47() == null ? "" : spaceSafetyTicket.getMeasure47());
            context.put("measure48", spaceSafetyTicket.getMeasure48() == null ? "" : spaceSafetyTicket.getMeasure48());
            context.put("measure49", spaceSafetyTicket.getMeasure49() == null ? "" : spaceSafetyTicket.getMeasure49());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return context;
    }

    //将Url转换为File
    private File UrltoFile(String url) throws Exception {
        HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
        httpUrl.connect();
        InputStream ins = httpUrl.getInputStream();
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID());//System.getProperty("java.io.tmpdir")缓存
        if (file.exists()) {
            file.delete();//如果缓存中存在该文件就删除
        }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;

    }

    private FireWorkMeasures getFireWorkMeasures(Integer fireWorkId,Integer measureId){
        QueryWrapper<FireWorkMeasures> fireWorkMeasuresQueryWrapper = new QueryWrapper<>();
        fireWorkMeasuresQueryWrapper.eq("fire_work_id", fireWorkId);
        fireWorkMeasuresQueryWrapper.eq("measure_id", measureId);
        fireWorkMeasuresQueryWrapper.last("limit 1");
        FireWorkMeasures fireWorkMeasures = fireWorkMeasuresService.getOne(fireWorkMeasuresQueryWrapper);
        return fireWorkMeasures == null ? new FireWorkMeasures() : fireWorkMeasures;
    }

    private void generateSpaceTicket2(HttpServletResponse response, FireWork fireWork){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SpaceSafetyTicket2 spaceSafetyTicket = new SpaceSafetyTicket2();
        // 封装数据
        spaceSafetyTicket.setNumber(fireWork.getRecordNo()); //编号
        spaceSafetyTicket.setWorkApplyCompany(fireWork.getRegCompanyName()); //申请单位
        spaceSafetyTicket.setRegUserName(fireWork.getRegUserName()); //申请单位
        spaceSafetyTicket.setWorkApplyTime(fireWork.getCreateTime() == null ? "" : sdf.format(fireWork.getCreateTime())); //作业申请时间
        spaceSafetyTicket.setWorkContent(fireWork.getWorkContent()); //作业内容
        spaceSafetyTicket.setWorkCompany(fireWork.getRegCompanyName()); //作业单位

        spaceSafetyTicket.setSupervisionSign(imgurl + fireWork.getSafeSignImg()); //督导员签字
        spaceSafetyTicket.setProductManagerSign(imgurl + fireWork.getCheckSignImg()); //生产经理签字
        spaceSafetyTicket.setAcceptSign(imgurl + fireWork.getCheckAcceptSignImg()); //安全督导员签字

        spaceSafetyTicket.setWorkHeightDepth(fireWork.getWorkHeight()); //深度高度
        spaceSafetyTicket.setWorkPosition(fireWork.getFireAddress()); //位置

        spaceSafetyTicket.setWorkManPost(""); //作业人岗位
        List<FireWorkUser> fireWorkUsers = fireWorkUserService.selectListByColumn("fire_work_id", fireWork.getId());
        if (fireWorkUsers != null && fireWorkUsers.size() > 0) {
            String woker = fireWorkUsers.stream().map(u -> u.getWorkerName()).collect(Collectors.joining(","));
            String post = fireWorkUsers.stream().map(u -> u.getIsOut() == 1 ? "外部作业人" : "内部作业人").collect(Collectors.joining(","));
            spaceSafetyTicket.setWorkMan(woker); //作业人
            spaceSafetyTicket.setWorkManPost(post); //作业人岗位
        }else {
            spaceSafetyTicket.setWorkMan(""); //作业人

        }
        spaceSafetyTicket.setEstimateWorkTimeRange((fireWork.getFireBeginTime() == null ? "" : sdf.format(fireWork.getFireBeginTime()))
                + "至" + (fireWork.getFireEndTime() == null ? "" : sdf.format(fireWork.getFireEndTime()))); //预估作业实施时间

        spaceSafetyTicket.setProductManagerSignTime(fireWork.getLastCheckTime() == null ? "" : sdf.format(fireWork.getLastCheckTime())); //生产经理签字时间
        spaceSafetyTicket.setAcceptSignTime(fireWork.getCheckAcceptTime() == null ? "" : sdf.format(fireWork.getCheckAcceptTime())); //安全督导员签字时间

        spaceSafetyTicket.setProductManagerOpinion(fireWork.getLastCheckState() == null ? ""
                : (fireWork.getLastCheckState() == 1 ? "同意作业。" : "不同意")); //生产经理意见

        spaceSafetyTicket.setAccept(fireWork.getCheckAcceptSignImg() != null ? "本次作业已按要求完成，并进行了现场整理，作业完毕后观察15分钟，无遗留隐患，可以验收" : ""); //完工验收

        spaceSafetyTicket.setMeasure54(getFireWorkMeasures(fireWork.getId(), 42).getAnswer());
        spaceSafetyTicket.setMeasure55(getFireWorkMeasures(fireWork.getId(), 43).getAnswer());
        spaceSafetyTicket.setMeasure56(getFireWorkMeasures(fireWork.getId(), 44).getAnswer());
        spaceSafetyTicket.setMeasure57(getFireWorkMeasures(fireWork.getId(), 45).getAnswer());
        spaceSafetyTicket.setMeasure58(getFireWorkMeasures(fireWork.getId(), 46).getAnswer());
        spaceSafetyTicket.setMeasure59(getFireWorkMeasures(fireWork.getId(), 47).getAnswer());

        downloadSpaceSignTable2(spaceSafetyTicket, response);

    }

    public void downloadSpaceSignTable2(SpaceSafetyTicket2 spaceSafetyTicket, HttpServletResponse response) {
        try {
            String sou = String.valueOf(System.currentTimeMillis());
            String tempFileName = "shouxiankongjianzuoyepiao_2.docx";

            String path = uploadPath + File.separator + "spaceSafetyTicket" + File.separator + tempFileName;
//            String path = "D:\\template\\动火安全作业票.docx";
            System.out.println("文件路径 :" + path);

            File template = new File(path);
            InputStream ins = new FileInputStream(template);

            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsImage("supervisionSign");
            metadata.addFieldAsImage("productManagerSign");
            metadata.addFieldAsImage("acceptSign");


//            metadata.addFieldAsImage("imageNotExistsAndRemoveImageTemplate", NullImageBehaviour.RemoveImageTemplate);
//            metadata.addFieldAsImage("imageNotExistsAndKeepImageTemplate", NullImageBehaviour.KeepImageTemplate);
            // 封装数据
            context = setSpaceWordValue2(context, spaceSafetyTicket, metadata);

            //创建字段元数据
//            String fileName = uploadPath + File.separator + "highSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date())
//                    + File.separator + "受限空间安全作业票_" + sou + ".docx";
//            String fileName = uploadPath + File.separator + "spaceSafetyTicket" + File.separator + "受限空间安全作业票_" + sou + ".docx";
            File filPath = new File(uploadPath + File.separator + "spaceSafetyTicket" + File.separator + DateUtils.getMonthStr(new Date()));
//            File filPath = new File("D:\\template\\" + DateUtils.getMonthStr(new Date()));
            if (!filPath.exists()) {
                filPath.mkdir();
            }
            String fileName = filPath + File.separator + "受限空间安全作业票_" + sou + ".docx";
            System.out.println("fileName: " + fileName);
            File file = new File(fileName);
            FileOutputStream out = new FileOutputStream(file);
            report.process(context, out);
            out.flush();
            out.close();
            ins.close();
            try {
                if (file != null) {
                    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    FileUtils.setAttachmentResponseHeader(response, file.getName());
                    FileUtils.writeToStream(file, response.getOutputStream());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private IContext setSpaceWordValue2(IContext context, SpaceSafetyTicket2 spaceSafetyTicket, FieldsMetadata metadata) {
        try {
            context.put("number", spaceSafetyTicket.getNumber() == null ? "" : spaceSafetyTicket.getNumber()); //编号
            context.put("regUserName", spaceSafetyTicket.getRegUserName() == null ? "" : spaceSafetyTicket.getRegUserName()); //申请人
            context.put("workApplyCompany", spaceSafetyTicket.getWorkApplyCompany() == null ? "" : spaceSafetyTicket.getWorkApplyCompany()); //申请单位
            context.put("workApplyTime", spaceSafetyTicket.getWorkApplyTime() == null ? "" : spaceSafetyTicket.getWorkApplyTime()); //作业申请时间
            context.put("workContent", spaceSafetyTicket.getWorkContent() == null ? "" : spaceSafetyTicket.getWorkContent()); //作业内容
            context.put("workManPost", spaceSafetyTicket.getWorkManPost() == null ? "" : spaceSafetyTicket.getWorkManPost()); //作业人岗位
            context.put("workMan", spaceSafetyTicket.getWorkMan() == null ? "" : spaceSafetyTicket.getWorkMan()); //作业人
            context.put("estimateWorkTimeRange", spaceSafetyTicket.getEstimateWorkTimeRange() == null ? ""
                    : spaceSafetyTicket.getEstimateWorkTimeRange()); //动火作业实施时间
            context.put("workHeightDepth", spaceSafetyTicket.getWorkHeightDepth() == null ? "" : spaceSafetyTicket.getWorkHeightDepth());
            context.put("workPosition", spaceSafetyTicket.getWorkPosition() == null ? "" : spaceSafetyTicket.getWorkPosition());


            if (StringUtils.isNotBlank(spaceSafetyTicket.getAcceptSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getAcceptSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //安全督导员验收
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("acceptSign", iImageProvider); //安全督导员验收
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("acceptSign", iImageProvider); //安全督导员验收
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getProductManagerSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getProductManagerSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("productManagerSign", iImageProvider); //确认人-责任人
                } catch (Exception e) {
//                    e.printStackTrace();
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("productManagerSign", iImageProvider); //生产经理
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("productManagerSign", iImageProvider); //生产经理
            }
            if (StringUtils.isNotBlank(spaceSafetyTicket.getSupervisionSign())) {
                try {
                    File file1 = UrltoFile(spaceSafetyTicket.getSupervisionSign());
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("supervisionSign", iImageProvider); //督导员
                } catch (Exception e) {
                    File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                    IImageProvider iImageProvider = new FileImageProvider(file1, false);
                    context.put("supervisionSign", iImageProvider); //督导员
                }
            } else {
                File file1 = UrltoFile(imgurl + DEFAULTIMGURL);
                IImageProvider iImageProvider = new FileImageProvider(file1, false);
                context.put("supervisionSign", iImageProvider); //督导员
            }

            context.put("productManagerOpinion", spaceSafetyTicket.getProductManagerOpinion() == null ? "" : spaceSafetyTicket.getProductManagerOpinion()); //getProductManagerOpinion
            context.put("accept", spaceSafetyTicket.getAccept() == null ? "" : spaceSafetyTicket.getAccept()); //完工验收

            context.put("productManagerSignTime", spaceSafetyTicket.getProductManagerSignTime() == null ? ""
                    : spaceSafetyTicket.getProductManagerSignTime()); //生产经理签字时间
            context.put("acceptSignTime", spaceSafetyTicket.getAcceptSignTime() == null ? "" : spaceSafetyTicket.getAcceptSignTime()); //完工验收签字时间

            context.put("measure54", spaceSafetyTicket.getMeasure54() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure54())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure54()) ? "×" : "")));
            context.put("measure55", spaceSafetyTicket.getMeasure55() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure55())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure55()) ? "×" : "")));
            context.put("measure56", spaceSafetyTicket.getMeasure56() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure56())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure56()) ? "×" : "")));
            context.put("measure57", spaceSafetyTicket.getMeasure57() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure57())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure57()) ? "×" : "")));
            context.put("measure58", spaceSafetyTicket.getMeasure58() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure58())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure58()) ? "×" : "")));
            context.put("measure59", spaceSafetyTicket.getMeasure59() == null ? "" : ("1".equals(spaceSafetyTicket.getMeasure59())
                    ? "√" : ("0".equals(spaceSafetyTicket.getMeasure59()) ? "×" : "")));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return context;
    }
}
